
public class WidgetComposition : IWidget {
  private IList<IWidget> children;
  public int Width {
    get { return children.Sum(x => x.Width); }
  }
}



using Patterns4Net.Attributes;
[Composite(typeof(IWidget))]
class WidgetComposition : IWidget, ICloneable {
  private IList<IWidget> children;
  public int Width {
    get { return children.Sum(x => x.Width); }
  } // ...
}



public class Composite : IPattern {
  public TypeDefinition Composite { get; set; }
  public TypeReference Component { get; set; }
  // The Name is required by IPatter interface
  public string Name { 
    get { return "Composite"; } 
  }
}




var mapper = new ModelMapper();
mapper.Class<RegisteredUser>(mapping =>
{
  mapping.Id(x => x.Id, 
    map => map.Column("MyClassId"));
  mapping.Property(x => x.Username, 
    map => map.Length(150));
});





mock.expects(once()).method("m")
  .with(stringContains("hello"));

  
  
  
// we want to work with the Composite role
this.Type(composite => composite.Composite)  
// we want to check all its non-private methods
.Methods(method => 
  method.IsPublic || method.IsProtected) 
// on each of them perform the following check
.Check((composite, method) => 
    method.CallsAnyOf(pattern.Component),
  (composite, method) => 
    "error in " + method.Name));

    
    
    
    
// check that declaring type is not sealed:
this.Type(pattern => 
  pattern.TargetMethod.DeclaringType)
  .Check(type => type.IsSealed == false,
         (pattern, type) => "...error message...");

// check that template method invokes at least 
// one virtual method or another template method:
this.If(pattern => 
  !pattern.TargetMethod.DeclaringType.IsSealed)
  .Method(pattern => pattern.TargetMethod)
  .Check(
    method => 
      method.GetMethodCalls().Any(
        call =>
          call.TargetObject != null &&
          call.TargetObject.IsThisParameter &&
          (IsTemplateMethod(call.Method) ||
           call.Method.IsOverrideable())),
    (pattern, method) => "...error message...");

  
  
  
  
  
using Patterns4Net.Attributes;
[Composite(typeof(IWidget))]
class WidgetComposition : IWidget, ICloneable {
  // ...





public abstract class InstructionMatcher {
  public virtual bool Found { get; set; }
  public abstract bool Matches(
    Instruction instruction, 
    out Instruction next);
  public virtual void Reset() { ... }
}